//
//  TJModifyNicknameController.m
//  Teleport
//
//  Created by xtjun on 2020/6/23.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJModifyNicknameController.h"

@interface TJModifyNicknameController ()<UITextFieldDelegate>

@property (weak, nonatomic) UITextField *nameTextField;
@property (weak, nonatomic) UIButton *areaButton;
@property (weak, nonatomic) UIButton *saveButton;


@end

@implementation TJModifyNicknameController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.navigationBar.title = @"修改昵称".localizedString;
    self.navigationBar.lineView.hidden = YES;

    UITextField *nameTextField = [UITextField tj_addToView:self.view];
    nameTextField.placeholder = @"请输入昵称".localizedString;
    nameTextField.font = UIFont_Px(15);
    nameTextField.text = TJUserInfo.readInfo.nickName;
    [nameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
  //  nameTextField.keyboardType = UIKeyboardTypeNumberPad;
    [nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(36);
        make.top.offset(KNavBarHight + 50);
        make.height.offset(34);
        make.right.offset(-36);
    }];
    
    UIView *lineView = [UIView tj_addToView:self.view];
    lineView.backgroundColor = UIColor_Hex(0xE5E5E5);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(36);
        make.right.equalTo(nameTextField);
        make.height.offset(1);
        make.top.equalTo(nameTextField.mas_bottom).offset(4);
    }];
    
    UIButton *saveButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(16) color:UIColor.whiteColor title:@"保存".localizedString];
    saveButton.backgroundColor = UIColor.tj_mainColor;
    [saveButton shearRoundedCornersWithRadiu:3];
    [saveButton addTarget:self action:@selector(didClickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(56);
        make.right.offset(-56);
        make.height.offset(44);
        make.top.equalTo(lineView.mas_bottom).offset(50);
    }];
 
    _nameTextField = nameTextField;
    _saveButton = saveButton;
    [self textFieldDidChange:_nameTextField];
}
 
- (void)textFieldDidChange:(UITextField *)textField {
    _saveButton.userInteractionEnabled = _nameTextField.text.replacingEmptyString.length;
    _saveButton.backgroundColor = (_saveButton.userInteractionEnabled) ? UIColor.tj_mainColor : [UIColor_Hex(0x666666) colorWithAlphaComponent:0.3];
}

- (void)didClickSaveButton {
  
    NSString *name = _nameTextField.text;
    if (!name.length) {
        [TJHud showAutoHideMessage:_nameTextField.placeholder];
        return;
    }
    
    NSDictionary *parameters = @{
        @"userId": [TJUserInfo.readInfo userId],
        @"nickName": name,
    };
      
    [self.view endEditing:YES];
    [self showloading];
    
    [TJNetworking.shareNetworking POSTNew:@"member/member/setNickName" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        [self hiddenloading];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError * _Nonnull error) {
        [self hiddenloading];
        [TJHud showAutoHideMessage:error.localizedFailureReason];
    }];
    
}


@end
